<?php

use Doraemon\model\PublishGuide;
use Doraemon\service\Blank;
use Doraemon\service\ContentLib;
use Doraemon\service\Guide;
use Doraemon\service\Photos;
use Doraemon\service\TopicActivity;
use Doraemon\tools\BlockType;
use Doraemon\tools\Log;

class UpdateGroupTagAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * 迁移hhzhome队列 - 更新group_tag字段
     */
    public function main()
    {
        Log::info('UpdateGroupTag操作接收到的订阅消息', $this->messageBody);

        if (!$this->messageBody['database']) throw new \Exception('未获取到数据库');
        if (!$this->messageBody['table']) throw new \Exception('未获取到数据表');
        if (!$this->messageBody['data']) throw new \Exception('未接收到数据');

        $sTable = $this->messageBody['table'];

        $sGuideTabel = 'hhz_guide';
        $sBlankTabel = 'hhz_blank';
        $sTopicActivityTable = 'hhz_topic_activity';
        $aPhotoTable = ['hhz_photos_0', 'hhz_photos_1', 'hhz_photos_2', 'hhz_photos_3'];

        if (in_array($sTable, $aPhotoTable, true)) {
            $sId = $this->messageBody['data']['id'];
            $iObjType = BlockType::TYPE_PHOTO;

        } elseif ($sGuideTabel === $sTable) {
            $sId = $this->messageBody['data']['id'];
            $iObjType = BlockType::TYPE_GUIDE;

        } elseif ($sBlankTabel === $sTable) {
            $sId = $this->messageBody['data']['bid'];
            $iObjType = BlockType::TYPE_BLANK;

        } elseif ($sTopicActivityTable === $sTable) {
            $sId = $this->messageBody['data']['id'];
            $iObjType = BlockType::TYPE_TOPIC_ACTIVITY;
        }
        $aInfo = $this->messageBody['data'];

        Log::info('检查数据：', [$aInfo,$iObjType]);

        if (!empty($aInfo) && isset($iObjType)) {
            $sOldGroupTag = $aInfo['group_tag'] ?? '';
            $sNewGroupTag = self::getGroupTagByInfo($aInfo, $iObjType);

            Log::info('检查数据GroupTag：', [$sOldGroupTag,$sNewGroupTag]);

            if($sOldGroupTag == $sNewGroupTag){
                return $this->setSuccess();
            }
            if (empty($sNewGroupTag) && $sOldGroupTag) {
                $this->updateGroupTagToSql($sId, '', $iObjType);
            } else {
                $this->updateGroupTagToSql($sId, $sNewGroupTag, $iObjType);
            }
        }

        $this->setSuccess();
    }

    /**
     * 根据不同字段获取group_tag
     */
    public static function getGroupTagByInfo($aInfo, $iObjType)
    {
        $aMainTag = $aAdminTag = $aTitleTag = [];
        if (isset($aInfo['main_tag']) && $aInfo['main_tag']) {
            $aMainTag = explode(",", $aInfo['main_tag']);
        }
        if (isset($aInfo['admin_tag']) && $aInfo['admin_tag']) {
            $aAdminTag = explode(",", $aInfo['admin_tag']);
        }
        if ($iObjType === BlockType::TYPE_TOPIC_ACTIVITY) {
            $aTitleTag[] = $aInfo['title'];
        }
        $aTag = array_merge($aMainTag, $aAdminTag, $aTitleTag);

        /** 分析获取新的tag */
        $aNewGroupTag = ContentLib::getGroupTagByTags($aTag, $iObjType);
        if (!empty($aNewGroupTag)) {
            $sNewGroupTag = implode(",", $aNewGroupTag);
        }
        return $sNewGroupTag ?? '';
    }

    /**
     * 更新group_tag
     */
    public function updateGroupTagToSql($sId, $sNewGroupTag, $iObjType)
    {
        Log::info('更新group_tag:',[$sId,$sNewGroupTag]);
        switch ($iObjType) {
            case BlockType::TYPE_GUIDE:
                $result = \Doraemon\model\Guide::updateGuide($sId, ['group_tag' => $sNewGroupTag]);
                PublishGuide::updateById($sId, ['group_tag' => $sNewGroupTag]);
                break;
            case BlockType::TYPE_BLANK:
                $result = \Doraemon\model\Blank::updateById($sId, ['group_tag' => $sNewGroupTag]);
                break;
            case  BlockType::TYPE_PHOTO:
                $result = \Doraemon\model\Photos::updatePhotoById($sId, ['group_tag' => $sNewGroupTag]);
                break;
            case BlockType::TYPE_TOPIC_ACTIVITY:
                $result = \Doraemon\model\TopicActivity::updateById($sId, ['group_tag' => $sNewGroupTag]);
                break;
            default:
                break;
        }
        return $result ?? false;
    }
}
